/*文件注释
 * ===============================================================================
 * 说明：
 * 作者：zengyy
 * 版本：v1.0
 * 创建日期：2013-05-27 22:18:23
 * 完成日期： 
 * 修改纪录：  
 * ===============================================================================
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Transactions;

namespace SRF.Admin
{
    /// <summary>
    /// 字典 Service
    /// </summary>
    public class DictionaryService
    {
        readonly Data.DBHelperBase dbhelper = Data.DBHelperFactory.GetDBHelper();

        /// <summary>
        /// 获取字典
        /// </summary>
        /// <param name="id">字典id</param>
        /// <returns>返回字典</returns>
        public Models.Dictionary Get(string id)
        {
            return dbhelper.GetQuery("admin.dictionary.get").SetParameter(new { Id = id }).UniqueResult<Models.Dictionary>();
        }
        /// <summary>
        /// 获取字典列表
        /// </summary>
        /// <returns>返回字典列表</returns>
        public IList<Models.Dictionary> List()
        {
            return dbhelper.GetQuery("admin.dictionary.list").List<Models.Dictionary>();
        }
        /// <summary>
        /// 保存字典
        /// </summary>
        /// <param name="post">字典</param>
        /// <returns>返回字典Id</returns>
        public string Save(Models.Dictionary dictionary)
        {
            if (string.IsNullOrEmpty(dictionary.Id))
            {
                dictionary.Id = Guid.NewGuid().ToString();
                dbhelper.GetQuery("admin.dictionary.insert").SetParameter(dictionary).Execute();
            }
            else
            {
                dbhelper.GetQuery("admin.dictionary.update").SetParameter(dictionary).Execute();
            }

            return dictionary.Id;
        }
        /// <summary>
        /// 删除字典
        /// </summary>
        /// <param name="id">字典Id</param>
        public void Delete(string id)
        {
            using (TransactionScope transationScope = new TransactionScope(TransactionScopeOption.Required))
            {
                dbhelper.GetQuery("admin.dictionary.delete").SetParameter("Id", id).Execute();

                transationScope.Complete();
            }
        }
    }
}